part of '../page.dart';

class _LexicalItemTile extends StatefulWidget {
  const _LexicalItemTile({
    required this.title,
    required this.translation,
    required this.progressInfo,
    required this.onPressed,
    required this.onLeadingPressed,
    required this.trailing,
    required this.showPremiumBadge,
  });

  final String title;
  final String translation;
  final String progressInfo;
  final Widget trailing;
  final VoidCallback? onPressed;
  final VoidCallback? onLeadingPressed;
  final bool showPremiumBadge;

  @override
  State<StatefulWidget> createState() => _LexicalItemTileState();
}

class _LexicalItemTileState extends State<_LexicalItemTile> {
  bool _pressed = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: _pressed ? CoreTheme.of(context).pressedTileColor : CoreTheme.of(context).tileColor,
      child: Row(
        children: [
          AppIconButton(
            width: 50,
            height: 54,
            onPressed: widget.onLeadingPressed,
            icon: Icon(
              Icons.volume_up_rounded,
              size: 26,
              color: CoreTheme.of(context).primaryColor,
            ),
          ),
          Expanded(
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTapDown: (tapDownDetails) {
                if (widget.onPressed != null) {
                  setState(() {
                    _pressed = true;
                  });
                }
              },
              onTapCancel: () {
                if (widget.onPressed != null) {
                  setState(() {
                    _pressed = false;
                  });
                }
              },
              onTap: () {
                if (widget.onPressed != null) {
                  widget.onPressed!();
                  Future.delayed(kTilePressEffectDuration, () {
                    _pressed = false;
                    if (mounted) {
                      setState(() {});
                    }
                  });
                }
              },
              child: Row(
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          constraints: const BoxConstraints(
                            minHeight: 34,
                          ),
                          child: Align(
                            alignment: Alignment.centerLeft,
                            child: Padding(
                              padding: const EdgeInsets.only(
                                top: 10,
                                bottom: 5,
                              ),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Flexible(
                                    child: Text.rich(
                                      TextSpan(
                                        children: [
                                          TextSpan(
                                            text: widget.title,
                                            style: CoreTheme.of(context).textTheme.tileBoldTitle,
                                          ),
                                          TextSpan(
                                            text: '  –  ${widget.translation}',
                                            style: CoreTheme.of(context).textTheme.tileTitle,
                                          ),
                                        ],
                                      ),
                                      textWidthBasis: TextWidthBasis.longestLine,
                                    ),
                                  ),
                                  if (widget.showPremiumBadge) ...[
                                    const TextTilePremiumBadgeGap(),
                                    const TilePremiumBadge(),
                                  ],
                                ],
                              ),
                            ),
                          ),
                        ),
                        Text(
                          widget.progressInfo,
                          style: CoreTheme.of(context).textTheme.tileSubtitle,
                        ),
                        const SizedBox(
                          height: 8,
                        ),
                      ],
                    ),
                  ),
                  widget.trailing,
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
